package com.bupt.liutong.admin.core.dto;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bupt.liutong.admin.core.dao.RoleDao;
import com.bupt.liutong.util.StringUtils;

public class Authenticator extends HashMap<String, List<Integer>> {
	
	private RoleDao roleDao;
	private boolean init = false;
	private Map<String, String> actionParam;
	
	private Authenticator() {
		actionParam = new HashMap<String, String>();
	}
	private static Authenticator auth = new Authenticator();
	public static Authenticator getInstance(){
		return auth;
	}
	
	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
		if (!init) {
			List<Role> roles = roleDao.getRoleModuleOper();
			for (Role role : roles) {
				String key = role.getActionPath();
				String params = role.getParams();
				actionParam.put(key, params);
				if (!StringUtils.isEmpty(params)) {
					String[] paramArr = params.split(",");
					String[] valArr = role.getMethod().split(",");
					key += "?";
					for (int i = 0; i < paramArr.length; i++) {
						if (i > 0)
							key += "&";
						key += paramArr[i] + "=" + valArr[i];
					}
				}
				if (!containsKey(key)) {
					put(key, new ArrayList<Integer>());
				}
				get(key).add(role.getId());
			}
		}
		init = true;
	}
	
	public String getActionParam(String key) {
		return actionParam.get(key);
	}
}
